<template>
  <div class="default-main ba-table-box page">
    <!-- 按钮与搜索 -->
    <el-row class="page-button">
      <el-button>Default</el-button>
      <el-button type="primary">Primary</el-button>
      <el-button type="success">Success</el-button>
      <el-button type="info">Info</el-button>
      <el-button type="warning">Warning</el-button>
      <el-button type="danger">Danger</el-button>
      <el-input style="width: 20%; margin: 0 0 0 auto" v-model="searchText" placeholder="搜索" :suffix-icon="Search"
        @keyup.enter="handleSearch" />
    </el-row>

    <!-- 表格 -->
    <el-table :data="page.list" stripe v-loading="tableLoading">
      <el-table-column show-overflow-tooltip prop="date" label="Date" width="150" />
      <el-table-column show-overflow-tooltip prop="name" label="Name" width="120" />
      <el-table-column show-overflow-tooltip prop="state" label="State" width="120" />
      <el-table-column show-overflow-tooltip prop="city" label="City" width="120" />
      <el-table-column show-overflow-tooltip prop="address" label="Address" />
      <el-table-column show-overflow-tooltip prop="zip" label="Zip" width="120" />
      <el-table-column show-overflow-tooltip fixed="right" label="操作" width="120">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="edit(scope.row)">
            编辑
          </el-button>

          <el-popconfirm title="确认删除当前数据?" @confirm="del(scope.row)">
            <template #reference>
              <el-button link type="danger" size="small"> 删除 </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination style="margin-top: 16px" :currentPage="page.current" :page-size="page.limit"
      :page-sizes="[10, 20, 50, 100]" background layout="prev, pager, next, jumper, ->, total" :total="page.total"
      @current-change="onTableCurrentChange"></el-pagination>

    <!-- 编辑弹窗 -->
    <el-dialog v-if="dialogVisible" v-model="dialogVisible" title="编辑" width="800px">
      <el-form ref="formRef" :model="formData" label-width="120px" class="demo-dynamic">
        <el-form-item prop="email" label="邮箱" :rules="rules">
          <el-input v-model="formData.email" />
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="submit(formRef)">确认</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { Search } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { ref, reactive, onMounted } from "vue";

const tableData = new Array(10).fill({
  date: "2016-05-03",
  name: "Tom",
  state: "California",
  city: "Los Angeles",
  address: "No. 189, Grove St, Los Angeles",
  zip: "CA 90036",
  tag: "Home",
});

const rules = [
  {
    required: true,
    message: "不能为空",
    trigger: "blur",
  },
];

const formRef = ref(null);

const dialogVisible = ref(false);
const tableLoading = ref(false);
const searchText = ref("");
const tableLayout = ref("fixed");
const page = reactive({
  current: 1,
  limit: 10,
  total: 0,
  list: [],
});

const formData = reactive({
  email: "",
});
// 删除
const del = (row) => {
  new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve({
        success: true,
      });
    }, 300);
  }).then((res) => {
    ElMessage.success(res.success ? "删除成功!" : "删除失败!");
  });
};

// 编辑
const edit = (row) => {
  dialogVisible.value = true;
  formData.email = row.email;
};
// 保存
const submit = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve({
            success: true,
          });
        }, 300);
      }).then((res) => {
        ElMessage.success(res.success ? "保存成功!" : "保存失败!");
        if (res.success) {
          dialogVisible.value = false;
        }
      });
    } else {
      return false;
    }
  });
};

// 搜索
const handleSearch = (e) => {
  e.target.blur();
  loadTableData();
};

// 修改分页
const onTableCurrentChange = (val) => {
  page.current = val;
  loadTableData();
};

// 查询表格数据
const loadTableData = () => {
  tableLoading.value = true;
  new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve({
        list: tableData,
        page: page.current++,
        total: 11,
      });
    }, 300);
  })
    .then((res) => {
      page.current = res.page;
      page.total = res.total;
      page.list = res.list;
    })
    .finally(() => {
      tableLoading.value = false;
    });
};

// 页面挂载
onMounted(() => {
  loadTableData();

  // 请求示例
  fetch("https://jsonplaceholder.typicode.com/posts")
    .then((response) => response.json())
    .then((data) => console.log(data))
    .catch((error) => console.error(error));
});
</script>
<style scoped>
.page {
  width: calc(100% - var(--ba-main-space) - var(--ba-main-space));
  padding: 16px;
  background: #ffffff;
}

.page-button {
  padding: 16px 16px 16px 0;
}
</style>
